<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
use Common\Model\ConfigModel;
use Common\Model\CaozuologModel;
class ConfigController extends AdminController {
	public function _initialize(){
		parent::_initialize();
	}
	/**
	 * 获取所有的配置项
	 */
	public function getAllConfig(){
	    $where['status'] = 1;
	    $where['key'] = array('NEQ','kefu_erweima');
	    $where['id'] = array('NEQ',31);
		$config = M('Config')->where($where)->select();
		$kefu_erweima = M('Config')->where(array('key' => 'kefu_erweima'))->find()['value'];
		$tuijian_erweima = M('Config')->where(array('key' => 'tuijian_erweima'))->find()['value'];
		$data = ['config'=>$config,'status'=>1,'kefu_erweima'=>$kefu_erweima,'tuijian_erweima'=>$tuijian_erweima];
		$this->ajaxReturn($data);
	}
	/**
	 * 修改配置项
	 */
	public function changeConfig(){
	    foreach ($_POST as $k=>$v){
	        $r[]=M('Config')->where(C("DB_PREFIX")."config.key='{$k}'")->setField('value',$v);
	    }
		/* $id = I('id');
		$name = I('name');
		$value = I('value');
		$M_CONFIG = new ConfigModel();
		if(!$M_CONFIG->getConfig($id)){
			$data = [
				'status'=>-1,
				'mst'=>'参数错误，未查询到此条数据'
			];
			$this->ajaxReturn($data);
		}
		$r = $M_CONFIG->changeConfig($id,$value); */
		if(!$r){
			$data = [
			'status'=>0,
			'msg'=>'服务器繁忙'
					];
		
		}else{
			$data = [
			'status'=>1,
			'msg'=>'修改成功'
					];
			$M_LOG = new CaozuologModel();
			$content = '修改配置项';
			$M_LOG->addLog($this->user['username'], $content);
		}
		$this->ajaxReturn($data);
	}
	/**
	 * 新增配置项
	 * 
	 */
	public function addConfig(){
		$key = I('key');
		$value = I('value');
		$name = I('name');
		if(empty($key)){
			$data = [
			'status'=>-1,
			'msg'=>'请输入键'
					];
			$this->ajaxReturn($data);
		}
		if(empty($value)){
			$data = [
			'status'=>-2,
			'msg'=>'请输入值'
					];
			$this->ajaxReturn($data);
		}
		if(empty($key)){
			$data = [
			'status'=>-3,
			'msg'=>'请输入名称'
					];
			$this->ajaxReturn($data);
		}
		$M_CONFIG = new ConfigModel();
		$r = $M_CONFIG->addConfig($key, $value, $name);
		if(!$r){
			$data = [
			'status'=>0,
			'msg'=>'服务器繁忙'
					];
		
		}else{
			$data = [
			'status'=>1,
			'msg'=>'新增成功'
					];
			$M_LOG = new CaozuologModel();
			$content = '新增配置项，id为'.$r;
			$M_LOG->addLog($this->user['username'], $content);
		}
		$this->ajaxReturn($data);
	}
}